﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IMedicare;
using System.Collections;
using BusinessLibary;
using System.IO;
using EntityLibrary;
using DevExpress.Utils;

namespace Medicare
{
    public partial class FrmDownLoadData : Form
    {
        public FrmDownLoadData()
        {
            InitializeComponent();
        }
        private IMedicare.IMedicare Medicare;
        private int index = 0;
        private YBFWSSInfoFacade fwssFacade;
        private YBZLInfoFacade zlxmFacade;
        private YBYPInfoFacade ypmlFacade;
        private YBBZInfoFacade bzmlFacade;
        private YBYPInfoData ypmlData;
        private YBZLInfoData zlxmData;
        private YBFWSSInfoData fwssData;
        private YBBZInfoData bzmlData;

        private DateTime dt;

        public DateTime Dt
        {
            get { return dateTimePicker1.Value; }
            set { dt = value; }
        }


        private void FrmDownLoadData_Load(object sender, EventArgs e)
        {

        }

        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            Medicare = IMedicareFactory.Instance.CreateIMedicare();
            ArrayList arraylist = new ArrayList();
            int i = Medicare.InitHisDLL(arraylist);
            if (i == -1)
            {
                MessageBox.Show("医保接口初始化失败!");
            }
            //initDLL = true;

            if (cboSelectMedType.SelectedIndex == 0)
            {
                if (DownLoadData("02"))
                {
                    MessageBox.Show("下载诊疗项目成功！");
                }

            }
            if (cboSelectMedType.SelectedIndex == 1)
            {
                //服务设施
                if (DownLoadData( "06"))
                {
                    MessageBox.Show("下载服务设施项目成功！");
                }
            }
            if (cboSelectMedType.SelectedIndex == 2)
            {
                //药品
                if (DownLoadData("01"))
                {
                    MessageBox.Show("下载药品项目成功！");
                }
            }
            if (cboSelectMedType.SelectedIndex == 3)
            {
                //病种
                if (DownLoadData( "04"))
                {
                    MessageBox.Show("下载病种项目成功！");
                }
            }
        }

        private bool DownLoadData(string itemid)
        {
          
            ArrayList list=new ArrayList();
            list.Add(itemid);
            list.Add(Dt);
            int i=Medicare.DownLoadDataFromMedicare(list);

            if (i==0)
            {
                return true;
            }
            return false;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath.ToString() + "\\YBDLOAD\\";
            index = cboSelectMedType.SelectedIndex;
            DirectoryInfo dr = new DirectoryInfo(path);
            FileInfo[] inf = dr.GetFiles("*.txt");
            string patten = "";
            if (index == 0)
            {
                patten = "ZLXM";
            }
            if (index == 1)
            {
                patten = "FWSS";
            }
            if (index == 2)
            {
                patten = "YPML";
            }
            if (index == 3)
            {
                patten = "BZXX";
            }
            string filename = string.Empty;
            foreach (FileInfo f in inf)
            {
                if (f.Name.IndexOf(patten) != -1)
                {
                    filename= f.Name;
                }
            }
            if (filename.Equals(string.Empty))
            {
                MessageBox.Show("没有找到该文件，请确定该文件的唯一性或者是否存在");
                return;
            }
            path += filename;
            ImportData(index, path);
        }
        private void ImportData(int index, string path)
        {
            this.gridControl1.DataSource = null;
            gridView1.Columns.Clear();

            if (index == 2)
            {
                #region		药品目录
                ypmlData = new YBYPInfoData();
                try
                {
                    using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open), System.Text.Encoding.GetEncoding("gb2312")))
                    {
                        String input;
                        while ((input = sr.ReadLine()) != null)
                        {
                            //Console.WriteLine(input)
                            //this.richTextBox1.Text+=input;
                            string[] ii = input.Split('\t');
                            DataRow row = ypmlData.Tables[0].NewRow();
                            row["YPBM"] = ii[1];
                            row["YPMC"] = ii[2];
                            row["SFLB"] = ii[10];
                            row["YPDJ"] = ii[12];
                            row["PYZJM"] = ii[7];
                            row["WBZJM"] = ii[8];
                            row["PRICE"] = ii[29];
                            row["JX"] = ii[13];
                            row["GG"] = ii[14];
                            row["UNIT"] = ii[26];
                            row["REMARK"] = ii[18];
                            row["PRE0"] = ii[0];//唯一索引
                            row["PRE1"] = ii[5];//开始时间

                            row["PRE2"] = ii[16];//终止时间
                            row["PRE3"] = ii[15];//使用频次
                            row["PRE4"] = ii[17];//批准文号

                            row["PRE5"] = ii[21];//特药标志


                            row["PRE6"] = ii[22];//单复方标志
                            row["PRE7"] = ii[35];//有效标志
                            row["PRE8"] = ii[19];//经办人
                            row["PRE9"] = ii[20];//经办日期

                            ypmlData.Tables[0].Rows.Add(row);
                        }
                        //Console.WriteLine ("The end of the stream has been reached.");
                        sr.Close();
                    }
                    this.gridControl1.DataSource = null;
                    this.gridControl1.DataSource = ypmlData.Tables[0];
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message, e);
                }


                #endregion
            }
            if (index == 0)
            {
                #region 诊疗项目
                zlxmData = new YBZLInfoData();
                try
                {
                    using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open), System.Text.Encoding.GetEncoding("gb2312")))
                    {
                        String input;
                        while ((input = sr.ReadLine()) != null)
                        {
                            string[] ii = input.Split('\t');
                            DataRow row = zlxmData.Tables[0].NewRow();

                            row["XMBM"] = ii[1];

                            row["XMMC"] = ii[3];
                            row["SFLB"] = ii[4];

                            row["SFXMDJ"] = ii[5];
                            row["PYZJM"] = ii[6];
                            row["WBZJM"] = ii[7];

                            row["PRICE"] = ii[16];
                            row["GG"] = ii[9];

                            row["DW"] = ii[14];
                            row["REMARK"] = ii[10];
                            row["FLAG"] = ii[18];
                            row["PRE0"] = ii[0];//唯一索引
                            row["PRE1"] = ii[2];//开始时间

                            row["PRE2"] = ii[8];//结束时间
                            row["PRE3"] = ii[13];//特检特治标志
                            row["PRE4"] = ii[15];//城镇居民自负比例

                            row["PRE5"] = ii[17];//城镇居民是否需要审批


                            row["PRE6"] = ii[11];//经办人
                            row["PRE7"] = ii[12];//经办日期
                            row["PRE8"] = "";
                            row["PRE9"] = "";



                            zlxmData.Tables[0].Rows.Add(row);
                        }
                        //Console.WriteLine ("The end of the stream has been reached.");
                        sr.Close();
                        
                        this.gridControl1.DataSource = zlxmData.Tables[0];
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message, e);
                }

                #endregion
            }
            if (index == 1)
            {
                #region 服务设施
                fwssData = new YBFWSSInfoData();
                try
                {
                    using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open), System.Text.Encoding.GetEncoding("gb2312")))
                    {
                        String input;
                        while ((input = sr.ReadLine()) != null)
                        {
                            string[] ii = input.Split('\t');
                            DataRow row = fwssData.Tables[0].NewRow();
                            row["FWSSBM"] = ii[1];
                            row["FWSSMC"] = ii[2];
                            row["SFLB"] = ii[3];
                            row["XMDJ"] = ii[14];//病床等级
                            row["DW"] = "";
                            row["PYZJM"] = ii[4];

                            row["WBZJM"] = ii[5];
                            row["PRICE"] = ii[8];//城镇居民最高限价
                            row["REMARK"] = ii[11];//备注
                            row["PRE0"] = ii[0];//索引
                            row["PRE1"] = ii[6];//开始时间

                            row["PRE2"] = ii[7];//终止时间
                            row["PRE3"] = ii[15];//医院等级
                            row["PRE4"] = ii[16];//医疗人员类别

                            row["PRE5"] = ii[12];//城镇居民自付比例


                            row["PRE6"] = ii[9];//经办人
                            row["PRE7"] = ii[10];//经办日期
                            row["PRE8"] = "";
                            row["PRE9"] = "";

                            fwssData.Tables[0].Rows.Add(row);
                        }
                        //Console.WriteLine ("The end of the stream has been reached.");
                        sr.Close();
                        this.gridControl1.DataSource = null;
                        this.gridControl1.DataSource = fwssData.Tables[0];
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message, e);
                }
                #endregion

            }
            if (index == 3)
            {
                #region 病种目录
                bzmlData = new YBBZInfoData();
                try
                {
                    using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open), System.Text.Encoding.GetEncoding("gb2312")))
                    {
                        String input;
                        while ((input = sr.ReadLine()) != null)
                        {
                            string[] ii = input.Split('\t');
                            DataRow row = bzmlData.Tables[0].NewRow();
                            row["JBBM"] = ii[1];
                            row["JBMC"] = ii[2];
                            row["JBDJ"] = ii[3];
                            row["JBZL"] = ii[4];
                            row["PYZJM"] = ii[6];
                            row["WBZJM"] = ii[7];

                            row["REMARK"] = ii[14];
                            row["PRE0"] = ii[0];
                            row["PRE1"] = ii[5];//疾病报销标志

                            row["PRE2"] = ii[8];//疾病最高支付标准
                            row["PRE3"] = ii[9];//工伤标志
                            row["PRE4"] = ii[10];//生育标志

                            row["PRE5"] = ii[11];//有效标志


                            row["PRE6"] = ii[15];//社会保险经办机构编码
                            row["PRE7"] = ii[16];//统筹区号码
                            row["PRE8"] = ii[12];//经办人
                            row["PRE9"] = ii[13];//经办日期




                            bzmlData.Tables[0].Rows.Add(row);
                        }
                        //Console.WriteLine ("The end of the stream has been reached.");
                        sr.Close();
                        this.gridControl1.DataSource = null;
                        gridView1.Columns.Clear();
                        this.gridControl1.DataSource = bzmlData.Tables[0];
                    }
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message, e);
                }

                #endregion
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (index == 2)
            {
                WaitDialogForm form = new WaitDialogForm();
                form.Caption = "正在保存，请稍候！";
                ypmlFacade = new YBYPInfoFacade();
                if (ypmlData.Tables[0].Rows.Count > 0)
                {
                    ypmlFacade.deleteEntity();
                    ypmlFacade.insertEntity(ypmlData);
                }
                form.Close();
            }
            if (index == 0)
            {
                WaitDialogForm form = new WaitDialogForm();
                form.Caption = "正在保存，请稍候！";
                zlxmFacade = new YBZLInfoFacade();
                if (zlxmData.Tables[0].Rows.Count > 0)
                {
                    zlxmFacade.deleteEntity();
                    zlxmFacade.insertEntity(zlxmData);
                }
                form.Close();
            }
            if (index == 1)
            {
                WaitDialogForm form = new WaitDialogForm();
                form.Caption = "正在保存，请稍候！";
                fwssFacade = new YBFWSSInfoFacade();
                if (fwssData.Tables[0].Rows.Count > 0)
                {
                    fwssFacade.deleteEntity();
                    fwssFacade.insertEntity(fwssData);
                }
                form.Close();
            }
            if (index == 3)
            {
                WaitDialogForm form = new WaitDialogForm();
                form.Caption = "正在保存，请稍候！";
                bzmlFacade = new YBBZInfoFacade();
                if (bzmlData.Tables[0].Rows.Count > 0)
                {
                    bzmlFacade.deleteEntity();
                    bzmlFacade.insertEntity(bzmlData);
                }
                form.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
